﻿using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices;
//using System.Collections.Generic;

public enum AnalyticsType
{
	AT_UNKNOWN = 0,
	AT_FLURRY = 1,
	AT_NATIVE = 2,
	AT_GOOGLE = 4,
	AT_QUANTCAST = 8,
};

public class AnalyticsSDKScript {

	/* Interface to native implementation */
	#if UNITY_IOS
	[DllImport ("__Internal")]
	private static extern void _StartSession(int types);

	[DllImport ("__Internal")]
	private static extern void _StartAllSessions();

	[DllImport ("__Internal")]
	private static extern void _EndSession();

	[DllImport ("__Internal")]
	private static extern void _SendEvent(string category, string action, string label, long value);

	[DllImport ("__Internal")]
	private static extern void _SendScreenEvent(string sreenName);

	[DllImport ("__Internal")]
	private static extern void _SetDebugMode(bool bIsDebug);

	[DllImport ("__Internal")]
	private static extern bool _GetDebugMode();
	#endif

	public void StartSession(int types) 
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
//			_StartSession(types);
			#if UNITY_ANDROID
			SSCAnalyticsAdapter.getInstance().startSession(types);
			#elif UNITY_IOS
			_StartSession(types);
			#endif
		}
	

	}

	public void StartAllSessions() 
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			SSCAnalyticsAdapter.getInstance().startAllSession();
			#elif UNITY_IOS
			_StartAllSessions();
			#endif
		}
	}

	public void EndSession()
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			SSCAnalyticsAdapter.getInstance().endSession();
			#elif UNITY_IOS
			_EndSession();
			#endif
		}
	}

	public void SendEvent(string action, string label)
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			SSCAnalyticsAdapter.getInstance().sendEvent(action,label);
			#elif UNITY_IOS
			_SendEvent("",action,label,0);
			#endif
//			_SendEvent("",action,label,0);
		}
	}
		
	public void SendEvent(string category, string action, string label, long value)
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
//			_SendEvent(category,action,label,value);
			#if UNITY_ANDROID
			SSCAnalyticsAdapter.getInstance().sendEvent(category,action,label,value);
			#elif UNITY_IOS
			_SendEvent(category,action,label,value);
			#endif
		}
	}

	public void SendScreenEvent(string screenName)
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
//			_SendScreenEvent(sreenName);
			#if UNITY_ANDROID
			SSCAnalyticsAdapter.getInstance().sendScreen(screenName);
			#elif UNITY_IOS
			_SendScreenEvent(screenName);
			#endif
		}
	}

	public void SetDebugMode(bool bIsDebug)
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			SSCAnalyticsAdapter.getInstance().setDebugMode(bIsDebug);
			#elif UNITY_IOS
			_SetDebugMode(bIsDebug);
			#endif
		}
	}

	public bool GetDebugMode()
	{
		// Call plugin only when running on real device
		if (Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.Android) {
			#if UNITY_ANDROID
			return SSCAnalyticsAdapter.getInstance().getDebugMode();
			#elif UNITY_IOS
			return _GetDebugMode();
			#endif
		}
		return true;
	}

}

